﻿@using CommandLib
@model BackEnd.Models.ReportTransactionLogModel
@{
    ViewBag.Title = "Transaction Log";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Transaction Log</h2>
<table width="50%">    
    <tr>
        <td class="editor-label">@Html.LabelFor(m=>m.Email)</td>
        <td>@Html.TextBoxFor(m=>m.Email)@Html.HiddenFor(m=>m.ListId)</td>
        <td class="editor-label">@Html.LabelFor(m=>m.SubscriptionId)</td>
        <td>@Html.DropDownListFor(m=>m.SubscriptionId, new SelectList(Model.ListSubscription, "SubscriptionID", "Title"), "----All----")</td>
    </tr>
    <tr>
        <td class="editor-label">@Html.LabelFor(m=>m.Status)</td>
        <td>
            <select id="Status" name="Status">
                <option>----All----</option>
				<option value="@Constants.TranctionLogStatus.START">START</option>
                <option value="@Constants.TranctionLogStatus.SUCCESS">SUCCESS</option>
                <option value="@Constants.TranctionLogStatus.ERROR">ERROR</option>
				<option value="@Constants.TranctionLogStatus.CANCEL">CANCEL</option>
            </select>
        </td>
        <td class="editor-label">@Html.LabelFor(m=>m.TypePay)</td>
        <td>
            <select id="TypePay" name="TypePay">
                <option>----All----</option>
                <option value="@Constants.TypePay.ONE_PAY">Onepay</option>
                <option value="@Constants.TypePay.PAY_PAL">Paypal</option>
            </select>
        </td>
    </tr>
    <tr>
        <td class="editor-label">@Html.LabelFor(m=>m.PartnerId)</td>
        <td>@Html.DropDownListFor(m=>m.PartnerId, new SelectList(Model.ListPartner, "Id", "PartnerCode"), "----All----")</td>
        <td class="editor-label">@Html.LabelFor(m=>m.TypeTransaction)</td>
        <td>
            <select id="TypeTransaction" name="TypeTransaction">
                <option>----All----</option>
                <option value="@Constants.TypeTransactionPayMent.Register">Register</option>
                <option value="@Constants.TypeTransactionPayMent.RegisterAgain">Register Again</option>
                <option value="@Constants.TypeTransactionPayMent.Renew">Renew</option>
            </select>
        </td>
    </tr>
    <tr>
        <td class="editor-label">
            @Html.LabelFor(m => m.FromDate) 
        </td>
        <td class="editor-field">
            @Html.TextBoxFor(m => m.FromDate)
        </td>
        <td class="editor-label">
            @Html.LabelFor(m => m.ToDate) 
        </td>
        <td class="editor-field">
            @Html.TextBoxFor(m => m.ToDate)
        </td>
    </tr> 
    <tr>
        <td></td>
        <td align="left">            
            <a class="bt_red" href="#" onclick="Search(1);" style="float:left"><span class="bt_red_lft"></span><strong>Search</strong><span class="bt_red_r"></span></a>  
        </td>
        <td></td>
        <td align="right">
            <a class="bt_red" href="#" onclick="DeleteSelectedItems();" style="float:left"><span class="bt_red_lft"></span><strong>Delete TransactionLog</strong><span class="bt_red_r"></span></a>  
        </td>
    </tr>
    <tr>
        <td colspan="4"></td>
    </tr>
    <tr>
        <td colspan="4">
            <div id="divToTalRecord">Total record :</div>
        </td>
    </tr>
</table>
<table id="rounded-corner">
    <thead>
        <tr>                    
            <th class="rounded-company" scope="col" style="width: 30px"></th> 
            <th class="rounded" scope="col" style="width: 10px"><input id="chkAll" onclick="javascript: SelectAll(this, 'ListId');" type="checkbox" /></th>
            <th class="rounded" scope="col" style="width: 30px">Id</th>    	
            <th class="rounded" scope="col" style="width: 100px">Partner</th>    					
            <th class="rounded" scope="col" style="width: 50px">First Name</th>
            <th class="rounded" scope="col" style="width: 50px">Last Name</th>
            <th class="rounded" scope="col" style="width: 200px">Email</th>            
            <th class="rounded" scope="col" style="width: 100px">Subscriptions</th>
            <th class="rounded" scope="col" style="width: 100px">Marketing</th>
            <th class="rounded" scope="col" style="width: 100px">Payment type</th>
            <th class="rounded" scope="col" style="width: 100px">Coupon Code</th>
            <th class="rounded" scope="col" style="width: 100px">Amount</th>
            <th class="rounded" scope="col" style="width: 100px">Request Time</th>
            <th class="rounded" scope="col" style="width: 100px">Respones Time</th>
            <th class="rounded" scope="col" style="width: 100px">Status</th>
            <th class="rounded-q4" scope="col">Message</th>
        </tr>
    </thead>  
</table>
<div id="divPager"></div>
<script src="~/Scripts/ckeditor/ckeditor.js"></script>
<script src="~/Scripts/ckeditor/adapters/jquery.js"></script> 
<script>
    $(function () {
        $('#FromDate').datepicker();
        $('#ToDate').datepicker();
    });
    function Search(PageIndex) {
        var URL = '/TransactionLog/Search';
        $.ajax({
            url: URL,
            type: "POST",
            data: {
                email: $('#Email').val(),
                subscriptionId: $('#SubscriptionId').val(),
                typePay: $('#TypePay').val(),                
                status: $('#Status').val(),
                partnerId : $('#PartnerId').val(),
                fromDate: $('#FromDate').val(),
                toDate: $('#ToDate').val(),
                typeTransaction: $('#TypeTransaction').val(),
                pageIndex: PageIndex,
                pageSize: 50
            },
            success: function (data) {
                $('#divToTalRecord').html('Total record :'+data.TotalRecord);
                TotalPage = Math.ceil(data.TotalRecord / 50);
                if (TotalPage > 0) {
                    $("#divPager").show();
                } else {
                    $("#divPager").hide();
                }
                $("tr[refer='trContent']").remove();
                var $table = $('#rounded-corner');
                $.each(data.ListItem, function (i, item) {
                    var strHtml = "";
                    var requestTime = item.RequestTime == null ? '' : myDateFormatter(parseInt(item.RequestTime.substr(6)));
                    var responesTime = item.ResponesTime == null ? '' : myDateFormatter(parseInt(item.ResponesTime.substr(6)));
                    strHtml += "<tr refer='trContent'>";     
                    strHtml += "<td> " + ((PageIndex-1)*50 + i+1) + " </td>";
                    strHtml += "<td><input type='checkbox' value='true' refer='chk' onclick=javascript:SelectOne('ListId') name='check' itemid='" + item.Id + "' id='check'></td>";
                    strHtml += "<td> " + item.Id + " </td>";
                    strHtml += "<td> " + item.PartnerCode + " </td>";
                    strHtml += "<td> " + item.FirstName + " </td>";
                    strHtml += "<td> " + item.LastName + " </td>";                    					
                    strHtml += "<td> " + item.Email + " </td>";                    
                    strHtml += "<td> " + item.Subscription + " </td>";
                    strHtml += "<td> " + item.Marketing + " </td>";   
                    if (item.TypePay == @Constants.TypePay.ONE_PAY) {
                        strHtml += "<td>Onepay</td>";
                    } else {
                        if (item.TypePay == @Constants.TypePay.PAY_PAL) {
							strHtml += "<td> Paypal </td>";
						}
						else
						{
							strHtml += "<td></td>";
						}
                    }
                    strHtml += "<td> " + item.CouponCode + " </td>";
                    strHtml += "<td> " + item.Amount + " </td>";                    
                    strHtml += "<td> " + requestTime + " </td>";
                    strHtml += "<td> " + responesTime + " </td>";
                    switch (item.Status) {
                        case @Constants.TranctionLogStatus.START:
                            strHtml += "<td>Start</td>";
                            break;
                        case @Constants.TranctionLogStatus.SUCCESS:
                            strHtml += "<td>Success</td>";
                            break;
                        case @Constants.TranctionLogStatus.ERROR:
                            strHtml += "<td>Error</td>";
                            break;
                        case @Constants.TranctionLogStatus.CANCEL:
                            strHtml += "<td>Cancel</td>";  
                            break;
                        default :
                            strHtml += "<td></td>";   
                    }
                    strHtml += "<td> " + item.Message + " </td>";
                    strHtml += "</tr>";
                    $table.append(strHtml);
                });
                $("#divPager").pager({ pagenumber: PageIndex, pagecount: TotalPage, buttonClickCallback: Search });
            },
            complete: function (o1, o2) {
            }
        });
    }
    
    function DeleteSelectedItems() {        
        if (confirm('Bạn có muốn xóa dữ liệu?')) {
            var hidSelected = $("input[id='ListId']");
            if (hidSelected.val() != "") {                
                $.ajax({
                    url: urlpath + 'TransactionLog/Delete',
                    type: 'POST',
                    data: {
                        listId: hidSelected.val()
                    },
                    success: function (data) {                        
                        alert('Bạn đã xóa dữ liệu thành công!');                        
                        Search(1);
                    },
                    complete: function (o1, o2) {
                    }
                });
            }
        }
    };
</script>
